package com.liuyz.print_;

import org.junit.Test;

import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintStream;
import java.io.PrintWriter;

public class 字符打印流 {

    @Test
    public void write_1() {
        PrintWriter printWriter = null;
        try {
            // 构建一个字符打印输出流
            PrintStream out = System.out;
            printWriter = new PrintWriter(System.out);
            printWriter.print("hello-张三");
            // 必须调用刷新方法才会输出
            printWriter.flush();
            // 切换输出方式（切换成文件输出）
            printWriter = new PrintWriter(new FileWriter("d:\\test.txt"));
            printWriter.print("hi-李四");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (null != printWriter) {
                printWriter.close();
            }
        }
    }
}
